package com.borrow.jiemihua.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.bean.MainHotBean;
import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2017/9/15 0015.
 */

public class SuperloanAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context;
    private List<MainHotBean.DataBean> dataBeanList;
    private ImageLoader loader;

    public SuperloanAdapter(Context context) {
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        if (dataBeanList == null || dataBeanList.size() == 0) {
            return 0;
        }
        return dataBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.main_recycler_item2, parent, false);
            holder = new ViewHolder();
            holder.iv_main_image = (ImageView) convertView.findViewById(R.id.iv_main_image);
            holder.tv_kded = (TextView) convertView.findViewById(R.id.tv_kded);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_lv = (TextView) convertView.findViewById(R.id.tv_lv);
            holder.tv_peonum = (TextView) convertView.findViewById(R.id.tv_peonum);
            holder.tv_dklx = (TextView) convertView.findViewById(R.id.tv_dklx);
            holder.tv_fktime = (TextView) convertView.findViewById(R.id.tv_fktime);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(dataBeanList.get(position).getLogo()).into(holder.iv_main_image);
        String maxM = String.valueOf(dataBeanList.get(position).getMaxAmount());
        String minM = String.valueOf(dataBeanList.get(position).getMinAmount());
        String manED = "";
        String minED = "";
        DecimalFormat df = new DecimalFormat("######0.0");
        if (!maxM.equals("")) {
            if (maxM.length() >= 5) {
                String str = String.valueOf(df.format(Double.parseDouble(maxM) / 10000));
                int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                if (ii > 0) {
                    manED = str + "万";
                } else {
                    manED = String.valueOf(Integer.parseInt(maxM) / 10000) + "万";
                }
            } else if (maxM.length() == 4) {
                String str = String.valueOf(df.format(Double.parseDouble(maxM) / 1000));
                int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                if (ii > 0) {
                    manED = str + "千";
                } else {
                    manED = String.valueOf(Integer.parseInt(maxM) / 1000) + "千";
                }
            } else if (maxM.length() < 4) {
                manED = maxM;
            }
        }
        if (!minM.equals("")) {
            if (minM.length() >= 5) {
                String str = String.valueOf(df.format(Double.parseDouble(minM) / 10000));
                int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                if (ii > 0) {
                    minED = str + "万";
                } else {
                    minED = String.valueOf(Integer.parseInt(minM) / 10000) + "万";
                }
//                        minED=String.valueOf(df.format(Double.parseDouble(minM)/10000))+"万";
            } else if (minM.length() == 4) {
                String str = String.valueOf(df.format(Double.parseDouble(minM) / 1000));
                int ii = Integer.parseInt(str.substring(str.length() - 1, str.length()));
                if (ii > 0) {
                    minED = str + "千";
                } else {
                    minED = String.valueOf(Integer.parseInt(minM) / 1000) + "千";
                }
//                        minED=String.valueOf(df.format(Double.parseDouble(minM)/1000))+"千";
            } else if (minM.length() < 4) {
                minED = minM;
            }
        }

        holder.tv_kded.setText(minM + "元" + "~" + maxM + "元");
        holder.tv_name.setText(dataBeanList.get(position).getName());
        String rate = "最低利率 <font color= '#ff4e4e'>" + dataBeanList.get(position).getRate() + "%</font> " + " 月";
        holder.tv_lv.setText(Html.fromHtml(rate));
        holder.tv_peonum.setText(String.valueOf(dataBeanList.get(position).getApplyForCount()));
        if (dataBeanList.get(position).getLendType() == 1) {
            holder.tv_dklx.setText("现金贷");
        } else if (dataBeanList.get(position).getLendType() == 2) {
            holder.tv_dklx.setText("信用贷");
        } else if (dataBeanList.get(position).getLendType() == 3) {
            holder.tv_dklx.setText("大额贷");

        }
        double time = dataBeanList.get(position).getQuickestTime() / 60;
        String timeF = "最快<font color= '#ff4e4e'>" + time + "小时</font>" + "放款";

        holder.tv_fktime.setText(Html.fromHtml(timeF));
        return convertView;
    }

    public void setData(List<MainHotBean.DataBean> mData) {
        this.dataBeanList = mData;
        notifyDataSetChanged();
    }

    class ViewHolder {
        private TextView tv_kded;
        private TextView tv_name;
        private TextView tv_lv;
        private TextView tv_peonum;
        private TextView tv_dklx;
        private TextView tv_fktime;
        private ImageView iv_main_image;
    }
}
